package com.metro.paperless.common.controller;

import com.metro.paperless.common.beans.vo.WebUserInfo;
import com.metro.paperless.common.utils.UploadUtil;
import com.metro.paperless.sys.beans.vo.SysAttachment;
import com.metro.paperless.sys.service.inf.ISysAttachmentService;
import com.metro.paperless.web.controller.BaseController;
import com.metro.core.vo.CommonResult;
import com.metro.core.vo.ResultCode;
import com.metro.core.vo.inf.IResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 文件上传入口
 *
 * @author xk
 **/
@Controller
@RequestMapping(value = "/common")
public class CommonController extends BaseController {

    @Autowired
    private ISysAttachmentService sysAttachmentService;

    // 文件上传
    @RequestMapping(value = "uploadFiles", method = { RequestMethod.POST })
    public @ResponseBody CommonResult<?> uploadFiles( HttpServletRequest request,HttpServletResponse response, String moduleDir) {
        WebUserInfo userInfo  = (WebUserInfo)SecurityUtils.getSubject().getPrincipal();
        CommonResult<SysAttachment> cr = null;
        try {
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            MultipartFile file = multipartRequest.getFile("the_files");

            cr = UploadUtil.uploadAttachment(file, moduleDir,request,response);
            String moduleName = ServletRequestUtils.getStringParameter(request,"moduleName", "UPS");
            if(ResultCode.SUCCESS.getCode().equals(cr.getCode())){
                SysAttachment entity = cr.getData();
                if(entity != null){
                    entity.setModuleName(moduleName);
                    IResult codeMsg = sysAttachmentService.save(entity);
                    if(!ResultCode.SUCCESS.equals(codeMsg)){
                        cr.setResult(ResultCode.UPLOAD_FAIL);
                    }
                }
            }
            return cr;
        } catch (Exception ex) {
            cr = new CommonResult<>();
            cr.setResult(ResultCode.UPLOAD_FAIL);
            return cr;
        }
    }

    // 文件下载
    @RequestMapping(value = "download", method = { RequestMethod.GET })
    public void download( HttpServletRequest request,HttpServletResponse response, String filePath) {
        try {
            sysAttachmentService.download(filePath, response);
        } catch (Exception ex) {
            log.error("下载文件出错", ex);
        }
        return ;
    }

    // 文件下载
    @RequestMapping(value = "download/{fileId}", method = { RequestMethod.GET })
    public void download( HttpServletRequest request,HttpServletResponse response, @PathVariable Long fileId) {
        try {
            sysAttachmentService.download(fileId, response);
        } catch (Exception ex) {
            log.error("下载文件出错", ex);
        }
        return ;
    }

}
